package com.itboye.mengwu.app.ui.mymeng;

import java.util.ArrayList;
import java.util.Observable;
import java.util.Observer;

import android.annotation.TargetApi;
import android.os.Build;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.itboye.core.alert.ByAlert;
import com.itboye.core.object.BaseActivity;
import com.itboye.core.object.ResultEntity;
import com.itboye.core.object.SpPublicParams;
import com.itboye.core.utils.SPUtils;
import com.itboye.mengwu.R;
import com.itboye.mengwu.data.model.FenSiBean;
import com.itboye.mengwu.data.model.FenSiBean.FenSiModel;
import com.itboye.mengwu.data.presenter.FenSiPresenter;
import com.itboye.mengwu.widget.XListView;
import com.itboye.mengwu.widget.XListView.IXListViewListener;
import com.itboye.shopcar_adapter.FollowAdapter;
import com.itboye.shopcar_adapter.MeGuanZhuAdapter;
import com.umeng.common.message.Log;

@TargetApi(Build.VERSION_CODES.HONEYCOMB)
public class ActivityGuanZhu extends BaseActivity implements Observer,
		IXListViewListener {
	// 自定义listview
	private XListView guanzhuListView;
	private String uid;
	private MeGuanZhuAdapter adapter;
	private FenSiPresenter bagPresenter;
	private TextView txt_title;
	int pageno = 1;
	int pagesize = 10;
	// 实体类
	// FenSiBean arrayList=new FenSiBean();
	FenSiBean model;
	// ArrayList<FenSiBean.FenSiModel> listarr = new
	// ArrayList<FenSiBean.FenSiModel>();
	private ImageView back;
	RelativeLayout nodata;

	@Override
	public int initView() {

		return R.layout.activity_myguanzhu;
	}

	@Override
	public void onMyCreate() {

		txt_title.setText("关注");
		// 刷新
		guanzhuListView.setXListViewListener(this);
		guanzhuListView.setPullLoadEnable(true);
		guanzhuListView.setPullRefreshEnable(true);
		guanzhuListView.setAdapter(adapter);
		showProgressDialog("数据加载中，请稍后...", true);
		bagPresenter = new FenSiPresenter(this);
		model = new FenSiBean();
		uid = SPUtils.get(ActivityGuanZhu.this, null, SpPublicParams.UID, "")
				+ "";
		model.setUid(uid);
		bagPresenter.update(model);
		back.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				finish();
			}
		});
	}

	@SuppressWarnings("unchecked")
	@Override
	public void update(Observable observable, Object data) {
		// TODO Auto-generated method stub
		ResultEntity entity = handlerError(data);
		model = new FenSiBean();
		if (entity.getEventType() == bagPresenter.FenSiSuccess) {
			model = (FenSiBean) entity.getData();
			Log.e("hu  model", model + "");
			adapter = new MeGuanZhuAdapter(model, ActivityGuanZhu.this);
			guanzhuListView.setAdapter(adapter);
			adapter.notifyDataSetChanged();
			if (model.getList() == null ||model.getCount()==0) {
				nodata.setVisibility(View.VISIBLE);
				guanzhuListView.setVisibility(View.GONE);
			} else {
				nodata.setVisibility(View.GONE);
				guanzhuListView.setVisibility(View.VISIBLE);
			}
			try {
				/**
				 * 当数据加载出来后dialog则消失
				 */
				closeProgressDialog();
			} catch (Exception e) {
				ByAlert.alert("只有那么点关注了...");
			}

			stopLoad();
		}
	}

	@Override
	public void onRefresh() {
		// TODO Auto-generated method stub
		pageno = 1;
		pagesize = 10;
		model.setUid(uid);
		bagPresenter.update(model);
	}

	@Override
	public void onLoadMore() {
		// TODO Auto-generated method stub
		pageno++;
		pagesize++;
		model.setUid(uid);
		bagPresenter.update(model);
	}

	public void stopLoad() {
		guanzhuListView.stopLoadMore();
		guanzhuListView.stopRefresh();
	}

}
